package reptile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/*1)	、查看指定URL的HTML源文档内容？
提示：使用URL类和URLConnection类

String url = “https://www.cnblogs.com/itboxue/p/10738810.html”;

*/
public class Test {
    public static void main(String[] args) {
        BufferedReader bufferedReader = null;
        StringBuffer buffer = new StringBuffer();//要分清是不是流，流是要在后面再创建，而其他的可以直接创建
        String content = null;
        try {
            URL url = new URL("https://www.taobao.com/");//URL类是用来获得目标的地址
            URLConnection urlConnection = url.openConnection();
            //url中的一个方法可以获得URLConnection这个类的实例，它是后来要使用的对象，就是获取到它的输入流
            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
            //一定要记得字符流与套接字或其他类的流连接
            while ((content = bufferedReader.readLine())!= null){
                buffer.append(content);
                buffer.append("\r\n");
            }
            System.out.println(buffer.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                    bufferedReader = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
